package com.itlong.cloud;

import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;


/**
 * Created by itlong on 2017/8/9.
 */
@Configuration
@EnableCaching
public class RedisClient extends CachingConfigurerSupport  {

    private static Logger logger = LoggerFactory.getLogger(RedisClient.class);
    @Value("${redis.host}")
    private String REDIS_HOST;
    @Value("${redis.port}")
    private Integer REDIS_PORT;
    @Value("${redis.password}")
    private String REDIS_PASSWORD;
    @Bean
    //@ConfigurationProperties(prefix="redis")
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    //@ConfigurationProperties(prefix="redis")
    public JedisConnectionFactory getConnectionFactory(){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(REDIS_HOST);
        factory.setPort(REDIS_PORT);
        factory.setPassword(REDIS_PASSWORD);
        JedisPoolConfig config = getRedisConfig();
        //后续变为可配置，先临时使用。
        config.setTestOnBorrow(true);//使用时进行扫描，确保都可用
        config.setTestWhileIdle(true);//Idle时进行连接扫描
        config.setTestOnReturn(true);//还回线程池时进行扫描
        config.setMaxIdle(100);
        config.setMaxTotal(1000);
        config.setMinEvictableIdleTimeMillis(60000);
        config.setTimeBetweenEvictionRunsMillis(30000);
        config.setMaxWaitMillis(60000);
        factory.setPoolConfig(config);
        return factory;
    }



    @Bean
    public RedisTemplate<?, ?> getRedisTemplate(){
        RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
        return template;
    }

    public static void main(String[] args) {}


}
